package org.example.rabbitmq.simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author Lenovo
 * @version 1.0
 * @description TODO
 * @date 25/7/2023 上午8:20
 */
// 简单模式的生产者
public class ProducerDemo {
    public static void main(String[] args) throws Exception {
        // 创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        // 设置主机
        connectionFactory.setHost("192.168.100.1110");
        // 设置端口号
        connectionFactory.setPort(5672);
        // 设置虚拟主机
        connectionFactory.setVirtualHost("/");
        // 设置用户名
        connectionFactory.setUsername("admin");
        // 设置密码
        connectionFactory.setPassword("admin");

        // 创建连接
        Connection connection = connectionFactory.newConnection();

        // 创建信道
        Channel channel = connection.createChannel();

        // 声明队列
        channel.queueDeclare("simple_queue",true,false,false,null);

        // 创建要发送的消息
        String message = "hello rabbitmq";
        // 发布消息
        channel.basicPublish("","simple_queue",null,message.getBytes());

        // 关闭资源
        channel.close();
        connection.close();
    }
}
